package com.gmq.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.websocket.Session;
import java.time.LocalDateTime;

//源数据处理器,结合TableField实现字段数据自动填充
//自动填充用户id和创建时间
@Component
@Slf4j
public class MyMetaObject implements MetaObjectHandler {
    @Autowired
    private BaseContext baseContext;

    @Override
    public void insertFill(MetaObject metaObject) {
        //设置需要填充的字段
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("createUser", baseContext.getThreadLocal());
        metaObject.setValue("updateUser", baseContext.getThreadLocal());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser", baseContext.getThreadLocal());
    }
}
